package com.usf.jprovider.schema.provider.list;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;

import com.usf.jprovider.constants.Constants;
import com.usf.jprovider.object.JObjectNode;
import com.usf.jprovider.object.env.Environment;
import com.usf.jprovider.object.type.JString;
import com.usf.jprovider.object.type.JType;
import com.usf.jprovider.schema.provider.AbstractProvider;

public final class ProviderList extends AbstractProvider{

	@XmlElement(name="provider") protected AbstractProvider[] providers;
	@XmlAttribute protected String separator;
	
	public ProviderList() {
		super();
		providers = new AbstractProvider[]{};
		separator = Constants.LIST_PROVIDER_SEPARATOR;
	}
	public AbstractProvider[] getProviders() {
		return providers;
	}
	
	@Override
	public JString create(Environment env, JObjectNode node) throws Exception {
		StringBuilder value = new StringBuilder();
		for(AbstractProvider provider : providers)
			value.append(provider.create(env, node) + separator);
		return new JString(value.toString());
	}	
	@Override
	public boolean checkValue(Environment env, JObjectNode node, JType<?> value) throws Exception {
		return true; // TODO check
	}
	
	@Override
	public boolean isDependent(JObjectNode node) throws Exception {
		boolean value = false;
		for(AbstractProvider provider : providers)
			value = value || provider.isDependent(node);
		return value;
	}
}
